﻿using UnityEngine;
using System.Collections;

namespace Room
{
    public delegate void OnClick();
    public class UIInnerButton : MonoBehaviour
    {
        public UIEventTrigger trigger;
        public GameObject obj;
        public TweenPosition _pos;
        public UISprite sprite;
        void Awake()
        {
            trigger = obj.GetComponent<UIEventTrigger>();
            if (trigger == null)
            {
                trigger = obj.AddComponent<UIEventTrigger>();
            }
        }

        public void Init(OnClick onClick)
        {
            trigger.onClick = new System.Collections.Generic.List<EventDelegate>();
            trigger.onClick.Add(new EventDelegate(() => 
            {
                onClick();
            }));
           
            if(onClick==null)
            {
                GameObject.Destroy(trigger);
                GameObject.Destroy(obj.GetComponent<BoxCollider>());
                //sprite.spriteName = "UI_button_wakuang";
            }
            else
            {
                //sprite.spriteName = "UI_button_yugou";
            }
        }

        public void SetSprite(string spriteName)
        {
            sprite.spriteName = spriteName;
        }
        public void Hide()
        {
            if (gameObject.activeSelf)
            {
                gameObject.SetActive(false);
            }
        }

        public void Show()
        {
            if (!gameObject.activeSelf)
            {
                gameObject.SetActive(true);
                _pos.enabled = true;
                _pos.ResetToBeginning();
                _pos.PlayForward();
            }
        }

    }
}
